<template>
	<view class="container">
		<view v-if="checkPermi(['puyu:strengthtest:add'])">
			<button class="uni-button" style="color:#ffffff;backgroundColor:#993399;borderColor:#993399;width: 100%;margin: 10rpx 0;"
				type="primary" @click="handleAddStrengthTest()">添加体能指标</button>
		</view>
		<uni-section title="搜索" type="line"></uni-section>
		<view class="query-form">
			<view class="query-item">
				<uni-combox :candidates="studentCodeRange" placeholder="选择学号" v-model="queryParams.studentCode" @input="onStudentCode"></uni-combox>
			</view>
			<view class="query-item">
				<uni-combox :candidates="studentNameRange" placeholder="选择学员" v-model="queryParams.studentName" @input="onStudentName"></uni-combox>
			</view>
			<view class="query-item" v-if="checkRole(['admin','manager'])">
				<uni-combox :candidates="coachNameRange" placeholder="选择教练" v-model="queryParams.coachName" @input="onCoachName"></uni-combox>
			</view>
			<view class="query-item">
				<button class="uni-button" size="mini" type="primary" @click="handleQuery">搜索</button>
			</view>
		</view>
		<uni-section :title="'体能测试:共 ' + total + ' 人次'" type="line"></uni-section>
		<uni-table ref="table" :loading="loading" border stripe emptyText="暂无更多数据">
			<uni-tr>
				<uni-th align="center">体能测试数据</uni-th>
				<uni-th width="180rpx" align="center">操作</uni-th>
			</uni-tr>
			<uni-tr v-for="(item, index) in strengthtestList" :key="index">
				<uni-td>
					<view class="name">日期: {{ parseTime(item.createTime, '{y}-{m}-{d}') }}</view>
					<view class="name">学号: {{ getStudentCode(item.studentId) }}</view>
					<view class="name">学员姓名: {{ getStudentName(item.studentId) }}</view>
					<view class="name">身高(cm): {{ item.height }}</view>
					<view class="name">体重(kg): {{ item.weight }}</view>
					<view class="name">摸高(cm): {{ item.touchHigh }}</view>
					<view class="name">立定(cm): {{ item.standingLeap }}</view>
					<view class="name">一分钟跳绳-单摇(n): {{ item.ropeSkipping }}</view>
					<view class="name">一分钟跳绳-双摇(n): {{ item.ropeSkippingDouble }}</view>
					<view class="name remark">{{ item.remark }}</view>
				</uni-td>
				<uni-td align="center">
					<view v-if="checkPermi(['puyu:strengthtest:edit'])">
						<button class="uni-button" size="mini" type="primary"
							@click="handleEditStrengthTest(item)">修改</button>
					</view>
					<view v-if="checkPermi(['puyu:strengthtest:remove'])">
						<button class="uni-button" size="mini" type="warn"
							@click="handleDeleteStrengthTest(item)">删除</button>
					</view>
				</uni-td>
			</uni-tr>
		</uni-table>
		<uni-pagination :page-size="queryParams.pageSize" :current="queryParams.pageNum" :total="total" @change="change"
			prev-text="前一页" next-text="后一页" />
		<view class="page-sum">
			<text>当前页：{{ queryParams.pageNum }}，数据总量：{{ total }} 条，每页数据：{{ queryParams.pageSize }} 条</text>
		</view>
		<view v-if="checkPermi(['puyu:strengthtest:add'])">
			<button class="uni-button" style="color:#ffffff;backgroundColor:#993399;borderColor:#993399;width: 100%;margin: 10rpx 0;"
				type="primary" @click="handleAddStrengthTest()">添加体能指标</button>
		</view>
	</view>
</template>

<script>
	import {
		checkPermi,
		checkRole
	} from "@/utils/permission";
	import { listStrengthtest, delStrengthtest } from "@/api/puyu/strengthtest";
	import { listStudents } from "@/api/puyu/student";
	import { listCoachs } from "@/api/puyu/coach";
	import { parseTime } from '@/utils/common';
	export default {
		data() {
			return {
				// 遮罩层
				loading: true,
				// 总条数
				total: 0,
				// 课时信息
				lesson: {},
				// 体能测试表格数据
				strengthtestList: null,
				// 学员表格数据
				studentList: null,
				// 教练数据
				coachList: null,
				// 查询参数
				queryParams: {
					pageNum: 1,
					pageSize: 5,
					coachId: null,
					studentId: null,
					studentCode: null,
					studentName: null,
					coachName: null,
				},
			}
		},
		computed: {
			studentCodeRange() {
				if (!this.studentList) {
					return [];
				}
				let arr = [];
				this.studentList.map(item => {
					arr.push(item.studentCode);
				});
				return arr;
			},
			studentNameRange() {
				if (!this.studentList) {
					return [];
				}
				let arr = [];
				this.studentList.map(item => {
					arr.push(item.studentName);
				});
				return arr;
			},
			coachNameRange() {
				if (!this.coachList) {
					return [];
				}
				let arr = [];
				this.coachList.map(item => {
					arr.push(item.coachName);
				});
				return arr;
			},
		},
		created() {
			// 获取学员下拉列表
			this.getStudentLsit();
			// 获取教练下拉列表
			this.getCoachList();
		},
		onShow() {
			this.getList();
		},
		methods: {
			checkPermi,
			checkRole,
			parseTime,
			/** 搜索按钮操作 */
			handleQuery() {
				this.queryParams.pageNum = 1;
				this.getList();
			},
			/** 查询课时点名列表 */
			getList() {
				this.loading = true;
				this.queryParams.orderByColumn = 'strengthTestId'; //查询字段是表格中字段名字
				this.queryParams.isAsc = 'descending'; //动态取值排序顺序
				listStrengthtest(this.queryParams).then(response => {
					this.strengthtestList = response.rows;
					this.total = response.total;
					this.loading = false;
				});
			},
			/** 查询学员列表 */
			getStudentLsit() {
				listStudents().then(response => {
					this.studentList = response.data;
				});
			},
			/** 查询教练用户列表 */
			getCoachList() {
				listCoachs().then(response => {
					this.coachList = response.data;
				});
			},
			// 根据studentId获取学号
			getStudentCode(studentId) {
				if (!this.studentList) {
					return '';
				}
				let student = this.studentList.filter(item => item.studentId == studentId)[0]
				return student?student.studentCode:'';
			},
			// 根据studentId获取学员姓名
			getStudentName(studentId) {
				if (!this.studentList) {
					return '';
				}
				let student = this.studentList.filter(item => item.studentId == studentId)[0]
				return student?student.studentName:'';
			},
			/** 选择学号 */
			onStudentCode(e) {
				if (!this.studentList) {
					return;
				}
				if (!e) {
					this.queryParams.studentId = null;
					this.queryParams.studentName = null;
					this.getList()
					return;
				}
				let student = this.studentList.filter(item => item.studentCode == e)[0];
				if (student) {
					this.queryParams.studentId = student.studentId;
					this.queryParams.studentName = student.studentName;
				}
			},
			/** 选择学员姓名 */
			onStudentName(e) {
				if (!this.studentList) {
					return;
				}
				if (!e) {
					this.queryParams.studentId = null;
					this.queryParams.studentCode = null;
					this.getList()
					return;
				}
				let student = this.studentList.filter(item => item.studentName == e)[0];
				if (student) {
					this.queryParams.studentId = student.studentId;
					this.queryParams.studentCode = student.studentCode;
				}
			},
			/** 选择教练姓名 */
			onCoachName(e) {
				if (!this.coachList) {
					return;
				}
				if (!e) {
					this.queryParams.coachId = null;
					this.getList()
					return;
				}
				let coach = this.coachList.filter(item => item.coachName == e)[0];
				if (coach) {
					this.queryParams.coachId = coach.coachId;
				}
			},
			/** 添加体能测试 */
			handleAddStrengthTest() {
				this.$tab.navigateTo('/pages/puyu/strength-test/add-strength-test');
			},
			/** 修改体能测试 */
			handleEditStrengthTest(item) {
				item.studentCode = this.getStudentCode(item.studentId);
				item.studentName = this.getStudentName(item.studentId);
				this.$tab.navigateTo('/pages/puyu/strength-test/edit-strength-test?item=' + encodeURIComponent(JSON.stringify(item)));
			},
			/** 删除体能测试 */
			handleDeleteStrengthTest(item) {
				const strengthTestIds = item.strengthTestId;
				this.$modal.confirm('是否确认删除体能测试ID为"' + strengthTestIds + '"的数据项？').then(function() {
				  return delStrengthtest(strengthTestIds);
				}).then(() => {
				  this.getList();
				  this.$modal.msgSuccess("删除成功");
				}).catch(() => {});
			},
			// 分页触发
			change(e) {
				this.$refs.table.clearSelection()
				this.getData(e.current)
			},
			// 获取数据
			getData(pageCurrent, value = '') {
				this.queryParams.pageNum = pageCurrent
				this.getList()
			},
		}
	}
</script>

<style lang="scss" scoped>
	/* #ifndef H5 */
	/* page {
	padding-top: 85px;
} */
	/* #endif */

	.page-sum {
		width: 100%;
		display: flex;
		align-items: center;
		justify-content: center;
	}

	.query-form {
		display: flex;
		flex-wrap: wrap;
		justify-content: flex-start;
		background-color: #ffffff;
		margin-bottom: 10px;
	}

	.query-item {
		margin: 5rpx;
		padding: 5rpx;
	}
	
	.query-item-value {
		margin-top: 5rpx;
		color: darkgray;
	}
	.remark {
		color: #993399;
		font-weight: bolder;
	}
</style>